<?php

/**
 * 用户控制器
 *
 * @author MrHan<hcj112@163.com>
 */
class UserController extends CommonController
{
    /**
     * 用户登录
     *
     * @param $username 用户名
     * @param $password 密码
     * @param $verify   验证码
     */
    public function login($username = '', $password = '', $verify = '')
    {
        if (IS_POST) {

            if (!check_verify($verify)) {
                $this->error('验证码不正确!');
            }

            //　实例化UserApi
            $user = A('User/User', 'Api');
            $uid = $user->login($username, $password);
            if (0 < $uid) {
                $member = D('Member');
                if ($member->login($uid)) {
                    $this->success('登录成功!', U('Blog/Index/index'));
                }

            } else {
                switch ($uid) {
                    case -1:
                        $error = '用户不存在或被禁用!';
                        break;
                    case -2:
                        $error = '密码错误!';
                        break;
                    default:
                        $error = '未知错误!';
                }
                $this->error($error);
            }

        } else {
            $this->display();
        }

    }

    /**
     * 用户注册
     *
     * @param string $username      用户名
     * @param string $password      密码
     * @param string $repassword    确认密码
     * @param string $email         邮箱
     * @param string $verify        验证码
     */
    public function register($username = '', $password = '', $repassword = '', $email = '', $verify = '')
    {

        if (IS_POST) {
            if (!check_verify($verify)) {
                $this->error('验证码不正确!');
            }

            if ($password != $repassword) {
                $this->error('两次密码不一致!');
            }

            $user = A('User/User', 'Api');
            $uid = $user->register($username, $password, $email);

            if (0 < $uid) {
                $this->success('注册成功');
            } else {
                $this->error($this->showRegError($uid));
            }


        } else {
            $this->display();
        }


    }


    /**
     * 获取用户注册错误信息
     *
     * @param int $code 错误代码
     * @return string   错误信息
     */
    public function showRegError($code = 0)
    {
        switch ($code) {
            case -1:
                $error = '用户名长度必须在5-15个长度！';
                break;
            case -2:
                $error = '用户名已被注册!';
                break;
            case -3:
                $error = '密码长度必须6-30个长度!';
                break;
            case -4:
                $error = '邮箱格式不正确!';
                break;
            case -5:
                $error = '邮箱已被注册';
                break;
            default:
                $error = '未知错误!';
        }

        return $error;
    }

    /**
     * 用户设置中心
     */
    public function setting()
    {
        $this->display();
    }


    public function updatePasswod()
    {
        if (IS_POST) {
            $uid = is_login();
            $oldpwd = I('post.old');
            $data['password'] = I('post.password');
            $repassword = I('post.repassword');

            empty($oldpwd) && $this->error('请输入原来密码');
            empty($data['password']) && $this->error('请输入新密码');
            empty($repassword) && $this->error('请输入确认密码');


            if ($data['password'] != $repassword) {
                $this->error('两次密码不一致!');
            }

            $user = A('User/User', 'Api');
            $data['id'] = $uid;
            $result = $user->updateUserInfo($data);
            if ($result['status']) {
                $this->success('修改密码成功！');
            } else {
                $this->error('修改密码失败!');
            }
        }
    }


    //上传图片
    public function saveavater() {
        $base_url = __ROOT__;
        $user_id = session('uid');
        $rs = array();
        switch ($this->_get('action')) {
            //上传临时图片
            case 'uploadtmp':
                $file = 'uploadtmp.jpg';
                @move_uploaded_file($_FILES['Filedata']['tmp_name'], $file);
                $rs['status'] = 1;
                $rs['url'] = $base_url . '/uploads/' . $file;
                //file_put_contents("D:\\sina1.txt",$rs['url'],FILE_APPEND);
                break;
            //上传切头像
            case 'uploadavatar':
                $input = file_get_contents('php://input');
                $data = explode('--------------------', $input);
                file_put_contents('./ff.jpg',$data[0]);
                //设置上传目录
//                $big = 'big';
//                $small = 'small';
//                //小图
//                $big = avatar_file_name($user_id, $big, true);
//                //大图
//                $small = avatar_file_name($user_id, $small, true);
//
//                //生成你要的文件路径和名字开始 $data 0为小，1为大 目前暂支持两个规格
//
//                @file_put_contents($small, $data[0]);
//                @file_put_contents($big, $data[1]);
//                //@file_put_contents($file_name2, $data[2]);
//                //@file_put_contents($file_name3, $data[3]);
//                //生成你要的文件路径和名字结束
//                //写到数据库
//                $pic_path = $small;
//                //写入成功了$rs['status'] = 1;
//                //演示用
//                session('pic_path', $pic_path);  //设置session
                //返回状态
                $rs['status'] = 1;

                break;
            default:
                $rs['status'] = -1;
        }

        print json_encode($rs);
    }


    //获取用户的头像
    public function user_avatar() {

        $USER_AVATAR = ($_SERVER['HTTPS'] == 'on' ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'];
        $uid = isset($_GET['uid']) ? $this->_get('uid') : 0;
        $size = isset($_GET['size']) ? $this->_get('size') : 'small';

        $file_name = avatar_file_name($uid, $size);
        $avatar = $USER_AVATAR . __ROOT__ . '/' . $file_name;
        $file_path = ROOT_PATH . '/' . $file_name;

        //判断是否有头像，没有用Images下面的默认头像
        if (file_exists($file_path)) {
            $avatar = $USER_AVATAR . __ROOT__ . '/' . $file_name;
        } else {
            $no_avatar_url = 'Public/Images/noavatar_' . $size . '.png';
            $avatar = $USER_AVATAR . __ROOT__ . '/' . $no_avatar_url;
        }
        header('Location: ' . $avatar);
        exit;
    }


}